<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class user extends MY_Controller {
	function __construct(){
		parent::__construct();
	}

	function index(){
		if(!$this->ion_auth->logged_in()){
			redirect(base_url("user/login"),"refresh");
		}else{
			if($this->ion_auth->in_group("admin")){
				redirect(base_url("admin"),"refresh");
			}
			PageUtil::addVar("title",$this->config->item("site_title"));
			$this->_assign("userdata",$this->ion_auth->user_array());
			$this->_fetch('index');
		}
	}

	function login(){
		if($this->ion_auth->logged_in()){
			redirect(base_url("user"),"refresh");
		}else{
			PageUtil::addVar("title","เข้าสู่ระบบ - ".$this->config->item("site_title"));
			$this->_fetch("login");
		}
	}

	function checklogin(){
		if($this->ion_auth->logged_in()){
			redirect(base_url("user"),"refresh");
		}
		$_post = $this->input->post();

		//Check input
		if(empty($_post["username"])){
			$this->session->set_flashdata('error_message', "กรุณากรอก Username และลองอีกครั้ง!");
			redirect(base_url("user/login"),"refresh");	
		}
		if(empty($_post["password"])){
			$this->session->set_flashdata('error_message', "กรุณากรอก Password และลองอีกครั้ง!");
			redirect(base_url("user/login"),"refresh");	
		}
		if(!empty($_post["rememberme"])){
			$_post["rememberme"] = true;
		}else{
			$_post["rememberme"] = false;
		}
		//Check username and password are match
		$result = $this->ion_auth->login($_post["username"],$_post["password"],$_post["rememberme"]);
		if($result){
			redirect(base_url("user"),"refresh");
		}else{
			$this->session->set_flashdata('error_message', "Username และ Password ไม่ตรงกัน โปรดลองอีกครั้ง!");
			redirect(base_url("user/login"),"refresh");
		}
	}

	function register(){
		if($this->ion_auth->logged_in()){
			redirect(base_url("user"),"refresh");
		}

		$_post = $this->input->post();

		if(empty($_post["submitbtn"])){
			PageUtil::addVar("title","สมัครสมาชิก");
			$this->_fetch("register");
		}else{
			$_post["phone"] = intval($_post["phone"]);
			$_post["mobile"] = intval($_post["mobile"]);
			$username = $_post["username"];
			$password = $_post["password"];
			$email = $_post["email"];
			unset($_post["username"]);
			unset($_post["password"]);
			unset($_post["email"]);

			$result = $this->ion_auth->register($username, $password, $email, $_post);
			if(!$result){
				log_message("error","Register User failed: ".$username.", ".$password.", ".$email);
			}
			redirect(base_url("user/login"),"refresh");
		}
	}

	function ajax_checkuser(){
		if(!IS_AJAX){
			show_404();
		}

		$checkusername = $this->ion_auth->username_check($this->input->get("username"));
		if($checkusername){
			$data['responseText'] = "false";
			echo $data['responseText'];exit;
		}else{
			$data['responseText'] = "true";
			echo $data['responseText'];exit;
		}
	}
	function ajax_checkemail(){
		if(!IS_AJAX){
			show_404();
		}

		$checkusername = $this->ion_auth->email_check($this->input->get("email"));
		if($checkusername){
			$data['responseText'] = "false";
			echo $data['responseText'];exit;
		}else{
			$data['responseText'] = "true";
			echo $data['responseText'];exit;
		}
	}

	function admin_list(){
		$datauser = $this->ion_auth->users()->result_array();

		$this->_assign('datauser',$datauser);
		$this->_fetch("admin_list");
	}

	function edituser($id = NULL){
		$currentUserdata = $this->ion_auth->user_array();
		if(empty($id)){
			$id = $currentUserdata["id"];
		}
		if($currentUserdata["id"] != $id && !$this->ion_auth->is_admin()){
			show_404();
		}
		$post = $this->input->post();
		if(!empty($post["submit"])){
			//Submit Form
			if(($currentUserdata["id"] != $post["id"]) AND !$this->ion_auth->is_admin()){
				$this->session->set_flashdata('error', 'Something gone wrong, please contact administrator.');
				redirect(base_url(),"refresh");
			}else{
				unset($post["username"]);
				if(empty($post["password"])){
					unset($post["password"]);
					unset($post["repassword"]);
				}else{
					if($post["password"] != $post["repassword"]){
						$this->session->set_flashdata('error', 'Password not match.');
						redirect(base_url("admin/user/add/".$post["id"]),"refresh");
					}
				}
				$result = $this->ion_auth->update($post["id"], $post);
				if($this->ion_auth->is_admin()){
					$currentGroup = $this->ion_auth->get_users_groups($post["id"])->result_array();
					foreach ($currentGroup as $key => $value) {
						$currentGroupArray[] = $value["id"];
					}
					$groupDeleted = array_diff($currentGroupArray, $post["groups"]);
					if(!empty($groupDeleted)){
						foreach ($groupDeleted as $keyGroupID => $valueGroupID) {
							$this->ion_auth->remove_from_group($valueGroupID, $post["id"]);
						}
					}
					if(!empty($post["groups"])){
						foreach ($post["groups"] as $keyGroupID => $valueGroupID) {
							$this->ion_auth->add_to_group($valueGroupID, $post["id"]);
						}
					}
				}
				if(!$result){
					$this->session->set_flashdata('error', $this->ion_auth->error());
				}else{
					$this->session->set_flashdata('success', $this->ion_auth->messages());
				}
				$userdata = $this->ion_auth->user_array($post["id"]);
				redirect(base_url("admin/user/add/".$userdata["id"]),"refresh");
			}
		}else{
			//Display edti form
			$userdata = $this->ion_auth->user_array($id);
			if($this->ion_auth->is_admin()){
				//Get Groups
				$userdata["group"] = $this->ion_auth->get_users_groups($id)->result_array();
				//Get All group
				$this->_assign("groups", $this->ion_auth->groups()->result_array());
			}
			unset($userdata["password"]);
			$this->_assign("data", $userdata);
			$this->_fetch("admin_edituser");
		}
	}

	function admin_delete($id = NULL){
		if(!$this->ion_auth->logged_in()){
			show_404();
		}
		if(empty($id) AND !$this->ion_auth->in_group("admin")){
			show_404();
		}
		$data["active"] = 0;
		$this->ion_auth->update($id, $data);
		redirect('admin/user');
	}


	function admin_unban($id = NULL){
if(!$this->ion_auth->logged_in()){
			show_404();
		}
		if(empty($id) AND !$this->ion_auth->in_group("admin")){
			show_404();
		}
		$data["active"] = 1;
		$this->ion_auth->update($id, $data);
		redirect('admin/user');
	}



	function logout(){
		if(!$this->ion_auth->logged_in()){
			redirect(base_url(),"refresh");
		}
		$this->ion_auth->logout();
		redirect(base_url(),"refresh");
	}

}